package com.mano.mall.elk.autoconfig;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mano.mall.elk.aop.AopLogAspect;
import com.mano.mall.elk.kafka.KafkaSender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.KafkaTemplate;

/**
 * @Author: zj
 * @Description:
 * @Date: Created in 13:17 2020/10/20
 * @Modified By:
 */

@Configuration
@EnableConfigurationProperties(ELKLogProperties.class)
@ConditionalOnClass(AopLogAspect.class)
//@ConditionalOnProperty(prefix = "mano.mall")
public class ELKLogAutoConfig {

    @Autowired
    private ELKLogProperties elkLogProperties;

    @Bean
    @ConditionalOnMissingBean({KafkaTemplate.class,KafkaSender.class})
    public KafkaSender<JSONObject> kafkaSender(){
        KafkaSender<JSONObject> sender = new KafkaSender<>();
        sender.setLogTopic(elkLogProperties.getTopic());;
        return sender;
    }

    @Bean
    @ConditionalOnMissingBean(AopLogAspect.class)
    public AopLogAspect aopLogAspect(){
        return new AopLogAspect();
    }
}
